#!/usr/bin/python3
from glob import glob
from subprocess import call
import subprocess, os, codecs
import os

from PyQt5.uic import compileUi

def build_ui():
    for uifile in glob("ui/*.ui"):
        pyfile = os.path.dirname(uifile)+ '/ui_'+os.path.splitext(os.path.basename(uifile))[0] + ".py"
        #if outdated(pyfile, uifile):
        print(uifile)
        pyfile = open(pyfile, "wt", encoding="utf-8")
        uifile = open(uifile, "rt", encoding="utf-8")
        compileUi(uifile, pyfile, from_imports=True)


def generate_and_build_resource():
    # create qrc file and compile all resources to binary code
    images = os.listdir('resources/icons')
    qss = os.listdir('resources/qss')

    with open('resources.qrc', 'w+') as f:
        f.write('<!DOCTYPE RCC>\n<RCC version="1.0">\n<qresource>\n')

        for item in images:
            f.write('<file alias="' + os.path.basename(item) + '">resources/icons/' + item + '</file>\n')

        for item in qss:
            f.write('<file alias="qss/' + os.path.splitext(os.path.basename(item))[0] + '">resources/qss/' + item + '</file>\n')

        f.write('</qresource>\n</RCC>')


    pipe = subprocess.Popen(r'pyrcc5  -o resources_rc.py resources.qrc', stdout=subprocess.PIPE, stdin=subprocess.PIPE,
                            stderr=subprocess.PIPE, creationflags=0x08)

   # os.remove("images.qrc")

if __name__ == "__main__":
    generate_and_build_resource()
    build_ui()

